import requests
from bs4 import BeautifulSoup


def get_novel_chapters():
    root_url = "https://www.17k.com/list/3513765.html"
    r = requests.get(root_url)
    r.encoding = "utf-8"
    data = []
    soup = BeautifulSoup(r.text, "html.parser")
    for link in soup.find("dl", class_="Volume").find("dd").find_all("a"):
        title = link.find("span").text.strip()
        data.append(("https://www.17k.com%s" % link["href"], title))
    return data


def get_chapter_content(url):
    r = requests.get(url)
    print(url)
    r.encoding = "utf-8"
    soup = BeautifulSoup(r.text, "html.parser")
    content = soup.find("div", id="readArea").find("div", class_="p").get_text()

    return content


novel_chapters = get_novel_chapters()
total = len(novel_chapters)
index = 0
for chapter in novel_chapters:
    index = index + 1
    print(index,total)
    url, title = chapter
    print(chapter)
    with open("%s.txt" % title, "w", encoding="utf-8") as fout:
        fout.write(get_chapter_content(url))
